package com.platform.enums;

import com.platform.comm.constants.EnumOption;
import com.platform.comm.enums.IDictEntry;
import com.platform.comm.enums.IEnumItem;
import com.platform.comm.enums.IEnumOption;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zq
 * @version 1.0
 * @date 2022/1/13 17:50
 */
@EnumOption(name = "itemCharFunction")
public enum ItemCharFunction implements IDictEntry, IEnumItem, IEnumOption<String> {
    /**
     * 条件
     */
    EQ("=", "精确匹配",6),
    NE("<>", "不匹配",7),
    LIKE("LIKE", "包含",4),
    NLIKE("NOT LIKE", "不包含",5),
    LLIKE("LIKE", "结尾是",3),
    RLIKE("LIKE", "开头是",2),
    N("IS NULL", "是空",0),
    NN("IS NOT NULL", "不是空",1),
    EMPTY(" = '' ","是空文本",8),
    NEMPTY(" != '' ","非空文本",9);

    ItemCharFunction(String operation, String title,int sortNum) {
        this.operation = operation;
        this.title = title;
        this.sortNum = sortNum;
    }
    public final String operation;
    public final String title;
    public final int sortNum;

    public String getName() {
        return operation;
    }

    public String getDesc() {
        return title;
    }

    @Override
    public String key() {
        return name();
    }

    @Override
    public String value() {
        return title;
    }

    @Override
    public Map<String, String> enums() {
        List<ItemCharFunction> collect = Arrays.stream(values()).collect(Collectors.toList());
        sort(collect);
        return collect.stream().collect(Collectors.toMap(ItemCharFunction::key, ItemCharFunction::value,
                (u, v) -> {
                    throw new IllegalStateException(String.format("Duplicate key %s", u));
                },
                LinkedHashMap::new));
    }

    private static void sort(List<ItemCharFunction> dataList){
        dataList.sort(Comparator.comparingInt(m -> m.sortNum));
    }


    @Override
    public String getKey() {
        return operation;
    }

    @Override
    public String getValue() {
        return title;
    }

    @Override
    public Object getData() {
        return this;
    }

    @Override
    public boolean is(String code) {
        return false;
    }
}
